package cn.tedu.hotel.serve.controller;

import cn.tedu.hotel.serve.pojo.dto.HotelComplainingDTO;
import cn.tedu.hotel.serve.pojo.dto.HotelScoreDTO;
import cn.tedu.hotel.serve.pojo.vo.HotelComplainingVO;
import cn.tedu.hotel.serve.pojo.vo.HotelScoreVO;
import cn.tedu.hotel.serve.security.UserLoginPrincipal;
import cn.tedu.hotel.serve.service.IHotelComplainingService;
import cn.tedu.hotel.serve.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/hotelComplains")
@Api(tags = "09. 酒店投诉管理模块")
@Slf4j
@Validated
public class HotelComplainingController {

    @Autowired
    IHotelComplainingService hotelComplainingService;

    @PostMapping("/user/addHotelScore")
    @ApiOperation("用户向酒店投诉")
    @PreAuthorize("hasAuthority('userRoot')")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addHotelComplain(
            @Validated HotelComplainingDTO hotelComplainingDTO,
            @ApiIgnore @AuthenticationPrincipal UserLoginPrincipal userLoginPrincipal
    ){
        hotelComplainingDTO.setUserId(userLoginPrincipal.getId());
        hotelComplainingDTO.setUsername(userLoginPrincipal.getUsername());
        log.debug("开始处理【前台用户向酒店投诉】的请求",hotelComplainingDTO);
        hotelComplainingService.addHotelComplain(hotelComplainingDTO);
        return JsonResult.ok();
    }

    @GetMapping("/user/getHotelComplainingListById")
    @ApiOperation("前台根据用户id查询用户的投诉信息")
    @PreAuthorize("hasAuthority('userRoot')")
    @ApiOperationSupport(order = 400)
    public JsonResult<List<HotelComplainingVO>> getHotelComplainingListById(
            @ApiIgnore @AuthenticationPrincipal UserLoginPrincipal userLoginPrincipal
            ){
        log.debug("开始处理【前台根据用户id：{}查询用户的投诉信息】的请求",userLoginPrincipal.getId());
        List<HotelComplainingVO> list =
                hotelComplainingService.getHotelComplainingListById(userLoginPrincipal.getId());
        log.debug("数据：{}",list);
        return JsonResult.ok(list);
    }


    @PostMapping("/admin/{id:[0-9]+}/updateHotelComplainingById/{feedbackContent}")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperation("后台根据投诉ID修改投诉状态并反馈内容")
    @ApiOperationSupport(order = 700)
    public JsonResult<Void> updateHotelComplainingById(
            @PathVariable @Range(min = 1,message = "请提交有效值") Long id,
            @PathVariable String feedbackContent
    ){
        log.debug("开始处理【后台根据投诉ID:{}修改投诉状态并反馈内容：{}】的请求",id,feedbackContent);
        hotelComplainingService.updateHotelComplainingById(id,feedbackContent);
        return JsonResult.ok();
    }

    @GetMapping("/admin/getAdminHotelComplaining")
    @ApiOperation("后台返回投诉列表数据")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 800)
    public JsonResult<List<HotelComplainingVO>> getAdminHotelComplaining(){
        log.debug("开始处理【后台返回投诉列表数据】的请求");
        List<HotelComplainingVO> list = hotelComplainingService.getAdminHotelComplaining();
        log.debug("数据：{}",list);
        return JsonResult.ok(list);
    }

    @PostMapping("/admin/{id:[0-9]+}/deleteAdminHotelComplainingById")
    @ApiOperation("后台根据投诉ID修改投诉状态并反馈内容")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 600)
    public JsonResult<Void> deleteAdminHotelComplainingById(
            @PathVariable @Range(min = 1,message = "请提交有效值") Long id
    ){
        log.debug("开始处理【后台根据投诉id：{}删除用户的投诉信息】的请求",id);
        hotelComplainingService.deleteAdminHotelComplainingById(id);
        return JsonResult.ok();
    }
}
